using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

public class RecordManager : MonoBehaviour
{
    public int m_RecordCount = 5;
    public static RecordManager Instance;
    public void RecordOne(EGameMode gameMode, int passTime)
    {
        string recordStr = PlayerPrefs.GetString(string.Format("Record_{0}", gameMode), string.Empty);
        List<int> recordArr = null;
        if (string.IsNullOrEmpty(recordStr))
        {
            recordArr = new List<int>();
        }
        else
        {
            recordArr = JsonConvert.DeserializeObject<List<int>>(recordStr);
        }
        bool insert = false;
        for (int i = 0; i < recordArr.Count; i++)
        {
            if (recordArr[i] > passTime)
            {
                recordArr.Insert(i, passTime);
                insert = true;
                break;
            }
        }
        if (insert == false)
        {
            recordArr.Add(passTime);
        }

        for (int i = recordArr.Count - 1; i >= m_RecordCount; i--)
        {
            recordArr.RemoveAt(i);
        }

        recordStr = JsonConvert.SerializeObject(recordArr);
        Debug.LogFormat("recordStr:{0}", recordStr);

        PlayerPrefs.SetString(string.Format("Record_{0}", gameMode), recordStr);
    }

    public List<int> GetRecord(EGameMode gameMode)
    {
        string recordStr = PlayerPrefs.GetString(string.Format("Record_{0}", gameMode), string.Empty);
        if (string.IsNullOrEmpty(recordStr)) return new List<int>();
        List<int> recordArr = JsonConvert.DeserializeObject<List<int>>(recordStr);
        return recordArr;
    }

    void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
}
